<?php
namespace App\Http\Controllers\ApiApp\Stock;

use App\DBs\Stock\BoardDB;
use Illuminate\Http\Request;

class BoardController extends BaseController
{
    /**
     * 库
     */

    protected $db;

    public function __construct()
    {
        parent::__construct();
        $this->db = new BoardDB();
    }

    /**
     * 素材列表
     */
    public function getSourceIndex(Request $request)
    {
        //接口权限值
        $access = $request->post('appAccess','');
        if (env('APP_ENV')!='dev' &&
            $access!=$this->db->getAccessLast()) {
            return $this->toAppJson(-1);
        }
        $uid = $request->post('uid',0);
        $bid = $request->post('bid',0);
        if (!$uid || !$bid) {
            return $this->toAppJson(-2);
        }
        $resDb = $this->db->getAppSourceIndex($uid, $bid);
        if ($resDb['code']!=0) {
            return $this->toAppJson(-6,array(),$resDb['msg']);
        }
        $dataArr = $resDb['data'];
        return $this->toAppJson(0, $dataArr);
    }

    /**
     * 增加，创板记录、素材忽略
     */
    public function setAdd(Request $request)
    {
        //接口权限值
        $access = $request->post('appAccess','');
        if (env('APP_ENV')!='dev' &&
            $access!=$this->db->getAccessLast()) {
            return $this->toAppJson(-1);
        }
        $uid = $request->post('uid',0);
        $plusId = $request->post('plusId',0); //创单ID
        if (!$uid || !$plusId) {
            return $this->toAppJson(-2);
        }
        //获取用户信息
        $resUser = $this->db->getUnameByUid($uid);
        //获取创单信息
        $resPlus = $this->db->getPlusOneById($plusId);
        if (!$resPlus) {
            return $this->toAppJson(-5,array(),'没有创单');
        }
        $vid = $resPlus['vid'];
        //库视频信息
        $resStock = $this->db->getStockOneById($vid);
        if (!$resStock || !$resStock['plusNum']) {
            return $this->toAppJson(-5,array(),'没有视频');
        }
        $sourceNum = $resStock['plusNum'];
        //看看创板和素材有没有
        $resBoardDb = $this->db->getAppOneByPlus($uid,$plusId);
        if ($resBoardDb['code']==0) {
            $dataArr = $resBoardDb['data'];
            $resSources = $this->db->getSourcesById($dataArr['id']);
            $dataArr['sourceList'] = $resSources;
            return $this->toAppJson(0, $dataArr);
        }
        //没有则重新添加
        $dataArr = array(
            'uid' => $uid,
            'uname' => $resUser,
            'vid' => $vid,
            'plus' => $plusId,
            'num' => $sourceNum,
        );
        $resDb = $this->db->setAdd($dataArr);
        if ($resDb['code']!=0) {
            return $this->toAppJson(-6,array(),$resDb['msg']);
        }
        //返回的信息
        $boardId = $resDb['data']['boardId'];
        $sourceIds = $resDb['data']['sourceIds'];
        $resBoard = $this->db->getBoardOneById($boardId);
        $resSources = $this->db->getSourcesById($boardId);
        $resBoard['sourceList'] = $resSources;
        $dataArr = $resBoard;
        return $this->toAppJson(0, $dataArr);
    }

    /**
     * 更新，图片、文字
     */
    public function setSource(Request $request)
    {
        //接口权限值
        $access = $request->post('appAccess','');
        if (env('APP_ENV')!='dev' &&
            $access!=$this->db->getAccessLast()) {
            return $this->toAppJson(-1);
        }
        $bsId = $request->post('bsId',0); //素材ID
        $type = $request->post('type',0);
        $link = $request->post('link',0);
        if (!$bsId || !in_array($type,[1,2]) || !$link) {
            return $this->toAppJson(-2);
        }
        //获取素材信息
        $resOne = $this->db->getSourceOneById($bsId);
        if (!$resOne) {
            return $this->toAppJson(-5,array(),'没有数据');
        }
        //更新数据
        $resDb = $this->db->setSource($bsId, $type, $link);
        if ($resDb['code']!=0) {
            return $this->toAppJson(-500, array(), '');
        }
        return $this->toAppJson(0);
    }
}